package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserCharitablePosition;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/9/1
 */
public class UserCharitablePositionDaoImpl extends TempDataObjectDao<UserCharitablePosition> {
    public static String getUserCharitablePositionSql = "select * from t_u_charitable_position where userId=?";

    public Map<Integer, UserCharitablePosition> getUserCharitablePositionMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserCharitablePosition> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserCharitablePositionSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserCharitablePosition userCharitablePosition = parse(rs);
                map.put(userCharitablePosition.getType(), userCharitablePosition);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUserCharitablePositionSql = "update t_u_charitable_position set charitableForUpgradeTimes=? where userId=? and type=?";
    @Override
    protected boolean updateData(UserCharitablePosition userCharitablePosition) {
        userCharitablePosition.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userCharitablePosition.getCharitableForUpgradeTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, userCharitablePosition.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitablePosition.getType()));
        if (getDb().executeNoneQuery(updateUserCharitablePositionSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserCharitablePositionSql = "insert into t_u_charitable_position(userId,type,charitableForUpgradeTimes) values (?,?,?)";
    @Override
    protected boolean addData(UserCharitablePosition userCharitablePosition) {
        userCharitablePosition.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCharitablePosition.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitablePosition.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, userCharitablePosition.getCharitableForUpgradeTimes()));
        if (getDb().executeNoneQuery(insertUserCharitablePositionSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserCharitablePosition parseData(ResultSet rs) throws SQLException {UserCharitablePosition userCharitablePosition = new UserCharitablePosition();
        userCharitablePosition.setUserId(rs.getLong("userId"));
        userCharitablePosition.setType(rs.getInt("type"));
        userCharitablePosition.setCharitableForUpgradeTimes(rs.getInt("charitableForUpgradeTimes"));

        return userCharitablePosition;
    }
}
